package com.sgb.kjwl.view.ui.fragment.home;

import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.BaseHeightViewPagerFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentEnterpriseoperationBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.BannerEntity;
import com.sgb.kjwl.pop.HomeEnterpriseEncyclopediaTypePopWindow;
import com.sgb.kjwl.pop.HomeEpAssetsTypePopWindow;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.manage.CompanyTradesListEntity;
import com.sgb.kjwl.model.entity.manage.EpAssestEntity;
import com.sgb.kjwl.model.entity.manage.EpAssestTypeEntity;
import com.sgb.kjwl.model.entity.manage.IPDetailEntity;
import com.sgb.kjwl.model.entity.manage.SpecialQualificationEntity;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntity;
import com.sgb.kjwl.model.entity.manage.TradeHonorEntity;
import com.sgb.kjwl.view.event.CategoryEvent2;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.adapter.manage.CompanyTradesAdapter;
import com.sgb.kjwl.view.ui.adapter.manage.EpAssetAdapter;
import com.sgb.kjwl.view.ui.adapter.manage.IPAdapter;
import com.sgb.kjwl.view.ui.adapter.manage.RecommendedEnterprisesAdapter;
import com.sgb.kjwl.view.ui.adapter.manage.SpecialQualificationAdapter1;
import com.sgb.kjwl.view.ui.adapter.manage.TradeHonorAdapter;
import com.sgb.kjwl.viewmodel.home.EnterpriseOperationModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import static android.view.View.GONE;
import static com.sgb.kjwl.view.ui.activity.manage.IPActivity.KEY_BRAND;


/**
 * Description:
 * Author cfk
 * Time
 * 企业经营的fragment
 */
public class EnterpriseOperationFragment extends BaseHeightViewPagerFragment {
    private FragmentEnterpriseoperationBinding binding;
    private int mItemType = -1;
    @Inject
    EnterpriseOperationModel viewModel;
    private List<PickSelectEntity> typeEnterpriseEncyclopediaDatas = new ArrayList<>();
    private List<PickSelectEntity> typeIntellectualPropertyRightDatas = new ArrayList<>();
    private List<EpAssestTypeEntity> typeEpAssestTypeDatas = new ArrayList<>();

    private SpecialQualificationAdapter1 specialQualificationAdapter1;
    private EpAssetAdapter mAssetAdapter;
    private TradeHonorAdapter tradeHonorAdapter;
    private CompanyTradesAdapter companyTradesAdapter;
    private RecommendedEnterprisesAdapter recommendedEnterprisesAdapter;
    private IPAdapter ipAdapter;

    private Map<String, Object> enterpriseEncyclopediaMap = new HashMap<>();
    private Map<String, Object> companyTradesMap = new HashMap<>();
    private Map<String, Object> specialqualificationmap = new HashMap<>();
    private Map<String, Object> epAssestmap = new HashMap<>();
    private Map<String, Object> tradeHonortmap = new HashMap<>();

    private String type = KEY_BRAND;


    private String mCurrentIndex="";
    private String cityNo, dynamicTypeNo, industryNameNo, provinceNo;
    private String serviceType;
    private List<String> intellectualPropertyCatalog;
    //企业资产的PopWindow弹窗
    private HomeEpAssetsTypePopWindow homeEpAssetsTypePopWindow;
    //企业大全的PopWindow弹窗
    HomeEnterpriseEncyclopediaTypePopWindow homeEnterpriseEncyclopediaTypePopWindow;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() == null) {
            return;
        }
        mItemType = getItemType();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(getActivity())) {
            EventBus.getDefault().register(getActivity());
        }
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    public static EnterpriseOperationFragment newInstance(int position, BannerEntity bannerEntity,boolean isRefresh) {
        EnterpriseOperationFragment fragment = new EnterpriseOperationFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM, bannerEntity);
        args.putInt(ARG_PARAM_POS, position);
        args.putBoolean(ARG_isRefresh, isRefresh);
        fragment.setArguments(args);
        return fragment;
    }

    private int getItemType() {
        int mItemType = -1;
        BannerEntity bannerEntity = (BannerEntity) getArguments().getSerializable(ARG_PARAM);
        switch (bannerEntity.getName()) {
            case "企业大全":
                mItemType = 0;
                break;
            case "专项资质":
                mItemType = 1;
                break;
            case "知识产权":
                mItemType = 2;
                break;
            case "专项资产":
                mItemType = 3;
                break;
            case "荣誉荣耀":
                mItemType = 4;
                break;
            case "经营动态":
                mItemType = 5;
                break;
        }
        return mItemType;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_enterpriseoperation, container, false);
        return binding.getRoot();
    }

    @Override
    public void initView() {
        super.initView();
        if (getArguments() == null) {
            return;
        }
        mItemType = getItemType();
        if (mItemType == -1) {
            return;
        }
        //点击更多按钮时
        binding.tvMore.setOnClickListener(v -> getMoreData());
        binding.tvGoModel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().postSticky(CategoryEvent2.make(EventConstant.EVENT_CATEGORY_CHOOSE2, 23));
            }
        });
        binding.tvGoModel1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().postSticky(CategoryEvent2.make(EventConstant.EVENT_CATEGORY_CHOOSE2, 23));
            }
        });
        //创建线性布局管理器对象
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        //recyclerView设置管理器
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = getResources().getDimensionPixelSize(R.dimen.dp1);
            }
        });
        binding.tvBlank.setVisibility(View.VISIBLE);
        switch (mItemType) {
            //企业大全
            case 0:
                //判断企业大全的PopWindow弹窗是否为空
                if (homeEnterpriseEncyclopediaTypePopWindow == null) {
                    //为空时，创建企业大全的PopWindow弹窗对象
                    homeEnterpriseEncyclopediaTypePopWindow = new HomeEnterpriseEncyclopediaTypePopWindow(getContext());
                }
                //企业大全的PopWindow弹窗默认选中第一个item
                homeEnterpriseEncyclopediaTypePopWindow.setDefItem(0);
                //当点击打开企业大全的PopWindow弹窗按钮
                binding.editView.setOnClickListener(v -> {
                    //判断数据是否为空，为空则停止操作
                    if (null == typeEnterpriseEncyclopediaDatas || typeEnterpriseEncyclopediaDatas.size() == 0)
                        return;
                    //当打开时，图标也随之改变（指尖往上）
                    binding.editView.setImageResource(R.mipmap.btn_expand3);
                    //设置企业大全的PopWindow弹窗数据
                    homeEnterpriseEncyclopediaTypePopWindow.setData(typeEnterpriseEncyclopediaDatas);
                    //打开企业大全的PopWindow弹窗
                    homeEnterpriseEncyclopediaTypePopWindow.showPopwindow(binding.editView, true);

                });
                //当点击企业大全的PopWindow弹窗中任意item时
                homeEnterpriseEncyclopediaTypePopWindow.setOnResultClick((key1, key2, key3) -> {
                    //企业大全的PopWindow弹窗按钮的图标恢复默认（指尖往下）并自动关闭PopWindow弹窗
                    binding.editView.setImageResource(R.mipmap.btn_expand2);
                    //tablayout获取到PopWindow弹窗中点击的结果，实现联动
                    binding.tabLayout.getTabAt(((int) key2)).select();
                });
                //当点击企业大全的PopWindow弹窗之外时
                homeEnterpriseEncyclopediaTypePopWindow.setOnDismissListener(() -> {
                    //企业大全的PopWindow弹窗按钮的图标恢复默认（指尖往下）并自动关闭PopWindow弹窗
                    binding.editView.setImageResource(R.mipmap.btn_expand2);
                });

                //判断企业大全适配器是否为空
                if (recommendedEnterprisesAdapter == null) {
                    //为空时，创建企业大全对象
                    recommendedEnterprisesAdapter = new RecommendedEnterprisesAdapter(getActivity(), null, true);
                }
                //recyclerView设置适配器
                binding.recyclerView.setAdapter(recommendedEnterprisesAdapter);
                //点击recyclerView的子条目时
                recommendedEnterprisesAdapter.setOnItemClick(new AdapterOnItemClick<TheEnterpriseOfEntity>() {
                    @Override
                    public void onItemClick(TheEnterpriseOfEntity entity, int position) {
                        if (!TextUtils.isEmpty(entity.getSupplier())){
                            WebsiteInfoActivity.start(getContext(),entity.getSupplier(),1);
                        }else {
                            Toast.makeText(getActivity(), "公司no为空", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            //专项资质
            case 1:
                //判断专项资质适配器是否为空
                if (specialQualificationAdapter1 == null) {
                    //为空时,创建专项资质适配器对象
                    specialQualificationAdapter1 = new SpecialQualificationAdapter1(getActivity(), null);
                }
                //recyclerView设置适配器
                binding.recyclerView.setAdapter(specialQualificationAdapter1);
                break;
            //知识产权
            case 2:
                //判断知识产权适配器是否为空
                if (ipAdapter == null) {
                    //为空时,创建知识产权适配器对象
                    ipAdapter = new IPAdapter(getActivity(), null);
                }
                binding.recyclerView.setAdapter(ipAdapter);
                break;
            //企业资产
            case 3:
                //判断企业资产的PopWindow弹窗是否为空
                if (homeEpAssetsTypePopWindow == null) {
                    //为空时,创建企业资产的PopWindow弹窗对象
                    homeEpAssetsTypePopWindow = new HomeEpAssetsTypePopWindow(getContext());
                }
                //企业资产的PopWindow弹窗对象默认选中第一个item
                homeEpAssetsTypePopWindow.setDefItem(0);
                //当点击打开企业资产的PopWindow弹窗按钮
                binding.editView.setOnClickListener(v -> {
                    //判断数据是否为空，为空则停止操作
                    if (null == typeEpAssestTypeDatas || typeEpAssestTypeDatas.size() == 0)
                        return;
                    //企业资产的PopWindow弹窗按钮点击后，图标随之改变（指尖朝上）
                    binding.editView.setImageResource(R.mipmap.btn_expand3);
                    //企业资产的PopWindow弹窗设置数据
                    homeEpAssetsTypePopWindow.setData(typeEpAssestTypeDatas);
                    //打开企业资产的PopWindow弹窗
                    homeEpAssetsTypePopWindow.showPopwindow(binding.editView, true);
                });
                //当点击企业资产的PopWindow弹窗中的子条目时
                homeEpAssetsTypePopWindow.setOnResultClick((key1, key2, key3) -> {
                    //企业资产的PopWindow弹窗按钮的图标恢复默认（指尖往下）并自动关闭PopWindow弹窗
                    binding.editView.setImageResource(R.mipmap.btn_expand2);
                    binding.tabLayout.getTabAt(((int) key2)).select();
                });
                //当点击企业资产的PopWindow弹窗之外时
                homeEpAssetsTypePopWindow.setOnDismissListener(() -> {
                    //企业资产的PopWindow弹窗按钮的图标恢复默认（指尖往下）并自动关闭PopWindow弹窗
                    binding.editView.setImageResource(R.mipmap.btn_expand2);
                });
                //判断企业资产适配器是否为空
                if (mAssetAdapter == null) {
                    //为空则，创建企业资产适配器对象
                    mAssetAdapter = new EpAssetAdapter(getContext(), null);
                }
                //recyclerView设置适配器
                binding.recyclerView.setAdapter(mAssetAdapter);
                break;
            //行业荣誉
            case 4:
                //判断行业荣誉适配器是否为空
                if (tradeHonorAdapter == null) {
                    //为空则，创建行业荣誉适配器对象
                    tradeHonorAdapter = new TradeHonorAdapter(getContext(), null);
                }
                //recyclerView设置适配器
                binding.recyclerView.setAdapter(tradeHonorAdapter);
                break;
            case 5:
                //判断企业动态适配器是否为空
                if (companyTradesAdapter == null) {
                    //为空则，创建企业动态适配器对象
                    companyTradesAdapter = new CompanyTradesAdapter(getContext(), null);
                }
                //recyclerView设置适配器
                binding.recyclerView.setAdapter(companyTradesAdapter);
                break;
        }
    }

    @Override
    protected void onLazyLoad() {
        switch (mItemType) {
            case 0://推荐企业
                //tabLayout设置显示
                binding.tabLayout.setVisibility(View.VISIBLE);
                //请求企业大全的类型列表
                viewModel.getEnterpriseEncyclopediaTypeList(mItemType);
                break;
            case 1://专项资质
                ////头部目录全部处于隐藏状态
                binding.llCatalog.setVisibility(View.GONE);
                //请求专项资质的数据列表（默认）
                viewModel.getSpecialQualificationList(specialqualificationmap, false,mItemType,getIsRefresh());
                break;
            case 2://知识产权
                //手动添加知识产权目录中的子条目数据
                intellectualPropertyCatalog = new ArrayList<>();
                intellectualPropertyCatalog.add("商标");
                intellectualPropertyCatalog.add("专利");
                intellectualPropertyCatalog.add("版权");
                //PopWindow弹窗按钮设置隐藏
                binding.editView.setVisibility(View.GONE);
                //顶部的目录设置显示
                binding.llCatalog.setVisibility(View.VISIBLE);
                //tablaout设置显示
                binding.tabLayout.setVisibility(View.VISIBLE);
                //tablaout设置标签模式为固定模式
                binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
                //for循环tablayout动态添加子条目
                for (int i = 0; i < intellectualPropertyCatalog.size(); i++) {
                    binding.tabLayout.addTab(binding.tabLayout.newTab().setText(intellectualPropertyCatalog.get(i)));
                }
                //layout设置添加改变监听
                binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        //获取到tablayout当前子条目的名字
                        type = tab.getText().toString();
                        //调用封装好的网络请求的方法
                        switchTabLayout();

                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {
                    }
                });
                //默认请求知识产权数据数据
                setIntellectualPropertyData(false);
                break;
            case 3://企业资产
                //头部目录全部处于显示
                binding.llCatalog.setVisibility(View.VISIBLE);
                //tablaout设置显示
                binding.tabLayout.setVisibility(View.VISIBLE);
                //PopWindow弹窗按钮设置显示
                binding.editView.setVisibility(View.VISIBLE);
                //请求企业资产的类型数据
                viewModel.getAssestTypes(mItemType);
                //判断是否为空
                if (!mCurrentIndex.equals("")) {
                    //不为空时，存入epAssestmap
                    epAssestmap.put("typeNo", mCurrentIndex);
                } else {
                    //为空时，清空epAssestmap，防止数据残留，导致请求数据不对
                    epAssestmap.clear();
                }
                //设置请求企业资产数据
                viewModel.getAssestList(epAssestmap, false,mItemType,getIsRefresh());
                break;
            case 4://行业荣誉
                //头部目录全部处于隐藏
                binding.llCatalog.setVisibility(View.GONE);
                //请求行业荣誉的列表数据
                viewModel.getHonorList(tradeHonortmap, false,mItemType,getIsRefresh());
                break;
            case 5://企业动态
                //头部目录全部处于隐藏
                binding.llCatalog.setVisibility(View.GONE);
                //请求企业动态的列表数据
                setCompanyTradesData(false);
                break;
        }
    }
    public void IsRefresh(boolean isRefresh) {
        setIsRefresh(isRefresh);
        switchTabLayout();
    }
    /**
     *  设置类型数据
     */
    public <T> void setTypeDatas(List<T> datas) {
        if (datas == null || datas.size() == 0) {
            binding.emptyView.setVisibility(View.VISIBLE);
            return;
        }else {
            binding.emptyView.setVisibility(GONE);
        }
        switch (mItemType) {
            //企业大全
            case 0:
                //转型为原来类型
                List<PickSelectEntity> newdatas0 = (List<PickSelectEntity>) datas;
                //清除list中的旧数据
                typeEnterpriseEncyclopediaDatas.clear();
                //list添加新数据
                typeEnterpriseEncyclopediaDatas.addAll(newdatas0);
                //设置服务类型默认参数
                serviceType = newdatas0.get(0).getNo();
                //for循环layout动态添加子条目
                for (int i = 0; i < typeEnterpriseEncyclopediaDatas.size(); i++) {
                    binding.tabLayout.addTab(binding.tabLayout.newTab().setText(typeEnterpriseEncyclopediaDatas.get(i).getProjectscale()));
                }
                //layout设置添加改变监听
                binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        //判断企业大全的类型PopWindow窗口是否为空
                        if (homeEnterpriseEncyclopediaTypePopWindow == null) {
                            //为空时，创建企业大全的类型PopWindow窗口对象
                            homeEnterpriseEncyclopediaTypePopWindow = new HomeEnterpriseEncyclopediaTypePopWindow(getContext());
                        }
                        //企业大全的类型PopWindow窗口默认设置选中第一个子条目
                        homeEnterpriseEncyclopediaTypePopWindow.setDefItem(tab.getPosition());
                        //判断企业大全的类型列表数据是否为空
                        if (typeEnterpriseEncyclopediaDatas != null) {
                            //子条目中对应的No值，用来识别类型
                            serviceType = typeEnterpriseEncyclopediaDatas.get(tab.getPosition()).getNo();
                            // //调用封装好的网络请求的方法
                            switchTabLayout();
                        }
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {
                    }
                });
                //请求列表数据
                setEnterpriseEncyclopediaData(false);
                break;
            //专项资质
            case 1:
                //知识产权
            case 2:
                //转型为原来类型
                List<PickSelectEntity> newdatas2 = (List<PickSelectEntity>) datas;
                //清除list中的旧数据
                typeIntellectualPropertyRightDatas.clear();
                //list添加新数据
                typeIntellectualPropertyRightDatas.addAll(newdatas2);
                //for循环layout动态添加子条目
                for (int i = 0; i < typeIntellectualPropertyRightDatas.size(); i++) {
                    binding.tabLayout.addTab(binding.tabLayout.newTab().setText(typeIntellectualPropertyRightDatas.get(i).getProjectscale()));
                }
                break;
            //企业资产
            case 3:
                //转型为原来类型
                List<EpAssestTypeEntity> newdatas3 = (List<EpAssestTypeEntity>) datas;
                //获取list的长度
                int num = newdatas3.size();
                //创建一个数据，长度比list多一位。
                String[] titles = new String[num + 1];
                //设置数值的第一位；
                titles[0] = "全部";
                //创建新的list集合
                ArrayList<EpAssestTypeEntity> epAssestTypeEntities = new ArrayList<>();
                ////设置新list的第一位；
                epAssestTypeEntities.add(new EpAssestTypeEntity("全部", ""));
                //for循环
                for (int i = 0; i < num; i++) {
                    //list的值取出存入数组中
                    titles[i + 1] = newdatas3.get(i).getName();
                    ////list的值取出存入新list中
                    epAssestTypeEntities.add(i + 1, newdatas3.get(i));
                }
                //将新list的数据赋值給企业资产类型类型数据中
                typeEpAssestTypeDatas = epAssestTypeEntities;
                //for循环
                for (int i = 0; i < titles.length; i++) {
                    //layout动态添加子条目
                    binding.tabLayout.addTab(binding.tabLayout.newTab().setText(titles[i]));
                }
                // //layout设置添加改变监听
                binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        //判断企业资产的类型PopWindow窗口是否为空
                        if (homeEpAssetsTypePopWindow == null) {
                            //为空时，创建企业资产的类型PopWindow窗口对象
                            homeEpAssetsTypePopWindow = new HomeEpAssetsTypePopWindow(getContext());
                        }
                        ////企业大全的类型PopWindow窗口默认设置选中第一个子条目
                        homeEpAssetsTypePopWindow.setDefItem(tab.getPosition());
                        //判断企业大全的类型列表数据是否为空
                        if (typeEpAssestTypeDatas != null) {
                            //子条目中对应的No值，用来识别类型
                            mCurrentIndex = typeEpAssestTypeDatas.get(tab.getPosition()).getNo();
                            //调用封装好的网络请求的方法
                            switchTabLayout();
                        }
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {
                    }
                });
                break;

        }
    }
    /**
     * 当前视图可见时
     */
    @Override
    public void onVisible() {
        super.onVisible();
        switchTabLayout();
//        switch (mItemType) {
//            //企业大全
//            case 0:
//                //加载企业大全的缓存数据
//                loadCacheData(recommendedEnterprisesAdapter);
//                break;
//            //专项资质
//            case 1:
//                //加载专项资质的缓存数据
//                loadCacheData(specialQualificationAdapter1);
//                break;
//            //知识产权
//            case 2:
//                //加载知识产权的缓存数据
//                loadCacheData(ipAdapter);
//                break;
//            // 企业资产
//            case 3:
//                //加载企业资产的缓存数据
//                loadCacheData(mAssetAdapter);
//                break;
//            //行业荣誉
//            case 4:
//                //加载行业荣誉的缓存数据
//                loadCacheData(tradeHonorAdapter);
//                break;
//            //企业动态
//            case 5:
//                //加载企业动态的缓存数据
//                loadCacheData(companyTradesAdapter);
//                break;
//        }
    }

    /**
     * 切换标签页布局时，触发此方法
     */
    private void switchTabLayout() {
        switch (mItemType) {
            //推荐企业
            case 0:
                //设置请求企业大全数据
                setEnterpriseEncyclopediaData(false);
                break;
            //专项资质
            case 1:
                ////设置请求专项资质数据
                viewModel.getSpecialQualificationList(specialqualificationmap, false,mItemType,getIsRefresh());
                break;
            //知识产权
            case 2:
                //设置请求知识产权数据
                setIntellectualPropertyData(false);
                break;
            //企业资产
            case 3:
                //判断是否为空
                if (!mCurrentIndex.equals("")) {
                    //不为空时，存入epAssestmap
                    epAssestmap.put("typeNo", mCurrentIndex);
                } else {
                    //为空时，清空epAssestmap，防止数据残留，导致请求数据不对
                    epAssestmap.clear();
                }
                //设置请求企业资产数据
                viewModel.getAssestList(epAssestmap, false,mItemType,getIsRefresh());
                break;
            //行业荣誉
            case 4:
                //设置请求行业荣誉数据
                viewModel.getHonorList(tradeHonortmap, false,mItemType,getIsRefresh());
                break;
            //企业动态
            case 5:
                //设置请求企业动态数据
                setCompanyTradesData(false);
                break;
        }
    }

    /**
     * 点击更多按钮时，触发的数据加载
     */
    private void getMoreData() {
        switch (mItemType) {
            //推荐企业
            case 0:
                //设置请求企业大全数据
                setEnterpriseEncyclopediaData(true);
                break;
            //专项资质
            case 1:
                //设置请求专项资质数据
                viewModel.getSpecialQualificationList(specialqualificationmap, true,mItemType,getIsRefresh());
                break;
            //知识产权
            case 2:
                //设置请求知识产权数据
                setIntellectualPropertyData(true);
                break;
            //企业资产
            case 3:
                //设置请求企业资产数据
                viewModel.getAssestList(epAssestmap, true,mItemType,getIsRefresh());
                break;
            //行业荣誉
            case 4:
                //设置请求行业荣誉数据
                viewModel.getHonorList(tradeHonortmap, true,mItemType,getIsRefresh());
                break;
            //企业动态
            case 5:
                //设置请求企业动态数据
                setCompanyTradesData(true);
                break;
        }
    }

    /**
     *  加载更多数据之后，判断数据的情况，对此作出相应判断
     */
    public <T> void updateData(BaseRecycleAdapter<T> adapter, List<T> list, boolean isLoadMore) {
        //判断是否加载更多
        if (isLoadMore) {
            //判断列表数据是否为空
            if (list != null && list.size() > 0) {
                //适配器设置数据
                adapter.addDatas(list);
                if (list.size() < 3) {
                    //当数据少于3个时，隐藏加载更多按钮
                    binding.lineMore.setVisibility(View.VISIBLE);
                    binding.tvMore.setVisibility(View.GONE);
                } else {
                    //当数据数量正常时，默认显示加载更多按钮
                    binding.lineMore.setVisibility(View.VISIBLE);
                }
            } else {
                //当数据返回为空时，隐藏加载更多按钮
                binding.lineMore.setVisibility(View.VISIBLE);
                binding.tvMore.setVisibility(View.GONE);
            }
        } else {
            //判断列表数据是否为空
            if (list != null && list.size() > 0) {
                //当有数据时，显示加载更多按钮，隐藏缺省界面
                binding.lineMore.setVisibility(View.VISIBLE);
                if (list.size() < 3) {
                    //当数据少于3个时，隐藏加载更多按钮
                    binding.tvMore.setVisibility(View.GONE);
                } else {
                    binding.tvMore.setVisibility(View.VISIBLE);
                }
                binding.emptyView.setVisibility(View.GONE);
            } else {
                //当有数据时，隐藏加载更多按钮，显示缺省界面
                binding.lineMore.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
            //适配器设置数据
            adapter.setDatas(list);
        }
    }

    /**
     * 数据列表请求完毕之后的复用展示方法
     */
    public <T> void showList(List<T> list, boolean isLoadMore) {
        switch (mItemType) {
            //推荐企业
            case 0:
                //显示企业大全列表
                showEnterpriseEncyclopediaList((List<TheEnterpriseOfEntity>) list, isLoadMore);
                break;
            //专项资质
            case 1:
                //显示专项资质列表
                showSpecialQualificationList((List<SpecialQualificationEntity>) list, isLoadMore);
                break;
            //知识产权
            case 2:
                //显示知识产权列表
                shoWintellectualPropertyRightList((List<IPDetailEntity>) list, isLoadMore);
                break;
            //企业资产
            case 3:
                //显示企业资产列表
                showEpAssestList((List<EpAssestEntity>) list, isLoadMore);
                break;
            //行业荣誉
            case 4:
                //显示行业荣誉列表
                showIndustryHonorList((List<TradeHonorEntity>) list, isLoadMore);
                break;
            //企业动态
            case 5:
                //显示企业动态列表
                showCompanyTrades((List<CompanyTradesListEntity>) list, isLoadMore);
                break;
        }
        binding.tvBlank.setVisibility(GONE);
        if (list.size() <= 2) {
            binding.tvMore.setVisibility(View.GONE);
        } else {
            binding.tvMore.setVisibility(View.VISIBLE);
        }
    }
    /**
     * 显示企业大全列表
     */
    public void showEnterpriseEncyclopediaList(List<TheEnterpriseOfEntity> list, boolean isLoadMore) {
        updateData(recommendedEnterprisesAdapter, list, isLoadMore);
    }
    /**
     * 显示专项资质列表
     */
    public void showSpecialQualificationList(List<SpecialQualificationEntity> list, boolean isLoadMore) {
        updateData(specialQualificationAdapter1, list, isLoadMore);
    }
    /**
     * 显示知识产权列表
     */
    public void shoWintellectualPropertyRightList(List<IPDetailEntity> list, boolean isLoadMore) {
        updateData(ipAdapter, list, isLoadMore);
    }
    /**
     * 显示企业资产列表
     */
    private void showEpAssestList(List<EpAssestEntity> list, boolean isLoadMore) {
        updateData(mAssetAdapter, list, isLoadMore);
    }
    /**
     * 显示行业荣誉列表
     */
    private void showIndustryHonorList(List<TradeHonorEntity> list, boolean isLoadMore) {
        updateData(tradeHonorAdapter, list, isLoadMore);
    }
    /**
     * 显示企业动态列表
     */
    private void showCompanyTrades(List<CompanyTradesListEntity> list, boolean isLoadMore) {
        updateData(companyTradesAdapter, list, isLoadMore);
    }
    /**
     * 设置请求知识产权数据
     */
    private void setIntellectualPropertyData(boolean isLodMore) {
        //创建map对象
        Map<String, Object> map = new HashMap<>();
        //map传参
        map.put("typesOfPropertyRights", type);
        map.put("limit", 3);
        viewModel.getIntellectualPropertyList(map, isLodMore,mItemType,getIsRefresh());
    }
    /**
     * 设置请求企业动态数据
     */
    public void setCompanyTradesData(boolean isRefresh) {
        //判断map对象是否为空
        if (companyTradesMap == null) {
            //为空则创建map对象
            companyTradesMap = new HashMap();
        }
        //map传参
        companyTradesMap.put("cityNo", cityNo);
        companyTradesMap.put("dynamicTypeNo", dynamicTypeNo);
        companyTradesMap.put("industryNameNo", industryNameNo);
        companyTradesMap.put("provinceNo", provinceNo);
        companyTradesMap.put("limit", 3);
        viewModel.getCompanyTradesList(companyTradesMap, isRefresh,mItemType);
    }
    /**
     * 设置请求企业大全数据
     */
    private void setEnterpriseEncyclopediaData(boolean isLoadMore) {
        //经度
        Double longitude = Double.valueOf("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, "0.000000")) ? "0.000000" : SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, "0.0"));
        //纬度
        Double latitude = Double.valueOf("".equals(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, "0.000000")) ? "0.000000" : SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, "0.0"));
        //判断map对象是否为空
        if (enterpriseEncyclopediaMap == null) {
            //为空则创建map对象
            enterpriseEncyclopediaMap = new HashMap();
        }
        //map传参
        enterpriseEncyclopediaMap.put("longitude", String.valueOf(longitude));
        enterpriseEncyclopediaMap.put("latitude", String.valueOf(latitude));
        enterpriseEncyclopediaMap.put("serviceType", serviceType);
        enterpriseEncyclopediaMap.put("onMap", "0");
        viewModel.getComList(enterpriseEncyclopediaMap, isLoadMore,mItemType,getIsRefresh());
    }
    /**
     * 封装复用，用于加载缓存数据
     */
    public <T> void loadCacheData(BaseRecycleAdapter<T> adapter) {
        List<T> data = adapter.getDatas();
        if (data != null && data.size() > 3) {
            List<T> tempList = new ArrayList<>();
            tempList.add(data.get(0));
            tempList.add(data.get(1));
            tempList.add(data.get(2));
            adapter.setDatas(tempList);
        }
    }
    public void   OnRequestFailure(){
        binding.tvBlank.setVisibility(GONE);
        binding.emptyView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
